package com.example.accountservice.controller;

import annotation.DisableBaseResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.accountservice.service.AccountService;
import generator.domain.Account;
import generator.support.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/account")
@CrossOrigin
@Slf4j
public class AccountController {
    @Autowired
    AccountService accountService;

    /**
     * 获取所有信息
     */
    @GetMapping("/getAll")
    public List<Account> getAllAccount(){
        log.info("AccountController -> getAllAccount");
        return accountService.list();
    }

    /**
     * 添加新的报销
     */
    @PostMapping("/add")
    @DisableBaseResponse
    public ResponseResult<Object> addAccount(@RequestBody Account account){
        log.info("AccountController -> addAccount");
        if (!("已提交".equals(account.getAccountStatus()) || "草稿".equals(account.getAccountStatus()))){
            return new ResponseResult<>(400, "添加信息有误");
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        account.setAccountTime(format.format(new Date()));
        // 查看编号是否重复
        QueryWrapper<Account> accountqw = new QueryWrapper<>();
        accountqw.eq("account_no", account.getAccountNo());
        List<Account> accounts = accountService.list(accountqw);
        if (!accounts.isEmpty()){
            account.setAccountNo(UUID.randomUUID().toString());
        }
        if (accountService.save(account)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "添加失败");
    }

    /**
     * 修改报销信息
     */
    @PostMapping("/edit")
    @DisableBaseResponse
    public ResponseResult<Object> editAccount(@RequestBody Account account){
        log.info("AccountController -> editAccount");
        System.out.println(account);
        // 查看编号是否重复
        QueryWrapper<Account> accountqw = new QueryWrapper<>();
        accountqw.eq("account_no", account.getAccountNo());
        List<Account> accounts = accountService.list(accountqw);
        if (!accounts.isEmpty()){
            account.setAccountNo(UUID.randomUUID().toString());
        }
        // 查看是否为已提交
        accountqw.clear();
        accountqw.eq("account_id", account.getAccountId());
        List<Account> list = accountService.list(accountqw);
        if (list.isEmpty()){
            return new ResponseResult<>(400, "该订单不存在");
        }
        if ("已提交".equals(list.get(0).getAccountStatus())){
            return new ResponseResult<>(400, "该订单已提交");
        }

        if (accountService.updateById(account)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "修改失败");
    }

    /**
     * 删除报销信息
     */
    @GetMapping("/delete/{id}")
    @DisableBaseResponse
     public ResponseResult<Object> removeAccount(@PathVariable String id){
        log.info("AccountController -> removeAccount");
        int aid = 0;
        try{
            aid = Integer.parseInt(id);
        } catch (NumberFormatException e){
            return new ResponseResult<>(400, "请输入正确的id");
        }
        if (accountService.removeById(aid)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "删除失败");
    }

    @PostMapping("/like")
    public List<Account> likeAccount(@RequestBody Account account){
        log.info("AccountController -> likeAccount");
        QueryWrapper<Account> accountqw = new QueryWrapper<>();
        if (account.getAccountType() != null && !"".equals(account.getAccountType())){
            accountqw.eq("account_type", account.getAccountType());
        }
        if (account.getAccountStatus() != null && !"".equals(account.getAccountStatus())){
            accountqw.eq("account_status", account.getAccountStatus());
        }
        return accountService.list(accountqw);
    }
}
